

import { createNode } from "./createNode"
import { Visitor, Node, Context } from "./node"

export class LiveRoomRedPacketInfoVisitor implements Visitor {
    key: string = `live_room_red_packet`
    heart_reg: RegExp = /i\s(\d+)/
    visitNode(node: Node, ctx: Context) {
        if (!node) return;
        if (node.className && node.className === 'androidx.viewpager.widget.ViewPager') {
            node.children.map(child => {
                this.visitRedPacket(child, ctx)
            })
            this.set(ctx, {
                close: () => node.parent.children[node.parent.children.length - 1].clickOrRect()
            })
        } else {
            node.children.map(c => c.visit(this, ctx))
        }
    }
    visitRedPacket(node: Node, ctx: Context) {
        const redPackets: RedPacket[] = this.get(ctx)?.redPackets || []
        const redCtx = new Context()
        this.visitRedPacketInfo(node, redCtx)
        const redPacket = redCtx.get('red_packet')
        redPacket && redPackets.push(redPacket)
        this.set(ctx, {
            redPackets
        })
    }
    visitRedPacketInfo(node: Node, ctx: Context) {
        if (node.id && node.id.endsWith('action_view')) {
            ctx.set(`red_packet`, {
                join: () => node.clickOrParent()
            })
        }
        if (node.id && node.id.endsWith('red_packet_reward_source_follow_image')) {
            ctx.set(`red_packet`, {
                follow: () => node.clickOrRect()
            })
        }
        if (node.id && node.id.endsWith(`common_red_packet_pop_prepare_reward_desc_view`)) {
            const res = node.text.match(/主播的(.*?)红包/)
            if (res && res.length) {
                const [, type] = res;
                ctx.set(`red_packet`, {
                    type: type
                })
            }
            const c = node.text.match(/.*?穿云箭红包/)
            if (c && c.length > 0) {
                ctx.set(`red_packet`, {
                    type: `穿云箭`
                })
            }
            ctx.set(`red_packet`, {
                title: node.text
            })
        }
        // if(node.id && node.id.endsWith('first_line_text_view')){
        //     ctx.set(`red_packet`, {
        //         title: node.text
        //     })
        // }
        if (node.id && node.id.endsWith('title_text_view')) {
            ctx.set(`red_packet`, {
                title: node.text
            })
        }
        if (node.id && node.id.endsWith(`red_packet_cover_reward_usual_count`)) {
            ctx.set(`red_packet`, {
                count: parseInt(node.text)
            })
        }
        if (node.id && node.id.endsWith(`red_packet_cover_reward_sub_pack_count`)) {
            const [, count] = node.text.split('x');
            ctx.set(`red_packet`, {
                total: parseInt(count)
            })
        }
        if (node.id && node.id.endsWith(`red_packet_cover_reward_usual_unit`)) {
            ctx.set(`red_packet`, {
                unit: node.text
            })
        }
        if (node.id && node.id.endsWith(`second_line_text_view`)) {
            ctx.set(`red_packet`, {
                desc: node.text
            })
        }
        node.children.map(c => this.visitRedPacketInfo(c, ctx))
    }
    get(ctx: Context, def: any = {}) {
        return ctx.get(this.key) || []
    }
    set(ctx: Context, val: object) {
        ctx.set(this.key, val)
    }
    toSecond(statusText?: string) {
        if (!statusText) return 0;
        const timeReg = /(\d\d)\:(\d\d)/
        const res = statusText.match(timeReg)
        if (res && res.length > 1) {
            const [, m1, s1] = res;
            const second = parseInt(m1) * 60 + parseInt(s1)
            return second;
        }
        return 0;
    }
}
export interface RedPacket {
    title: string;
    count: number;
    total?: number;
    unit: string;
    desc: string;
    type: string;
    // 参与
    join: () => boolean;
    // 关注 有可能没有
    follow?: () => boolean;
}
export interface RedPacketInfo {
    // 关闭弹框
    close: () => boolean;
    // 红包
    redPackets: RedPacket[];
}
export function getRedPacketInfo(): RedPacketInfo {
    const ctx = new Context();
    sleep(1000)
    const node = createNode()
    const liveInfoVisitor = new LiveRoomRedPacketInfoVisitor()
    liveInfoVisitor.visitNode(node, ctx)
    const roomInfo = liveInfoVisitor.get(ctx)
    return roomInfo;
}
